//
//  YAYIApilyDetailCell.m
//  YAYIDoctor
//
//  Created by hxp on 16/5/4.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIApilyDetailCell.h"

@implementation YAYIApilyDetailCell


- (void)setSection:(NSInteger)section
{
    _section = section;
    
    [self setNeedsLayout];
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createViews];
    }
    return self;
}


-(void)createViews
{
    self.title = [UILabel new];
    self.title.font = [UIFont systemFontOfSize:font14];
    self.title.textColor = [UIColor blackColor];
    [self.contentView addSubview:self.title];
    
    self.icon = [UIImageView new];
    self.icon.image = [UIImage imageNamed:@"accessIcon"];
    [self.contentView addSubview:self.icon];

    self.name = [UILabel new];
    self.name.textAlignment = NSTextAlignmentRight;
    self.name.font = [UIFont systemFontOfSize:font14];
    self.name.textColor = [UIColor blackColor];
    [self.contentView addSubview:self.name];

    self.textfield = [UITextField new];
    self.textfield.returnKeyType = UIReturnKeyDone;
    self.textfield.font = [UIFont systemFontOfSize:font14];
    self.textfield.textAlignment = NSTextAlignmentRight;
    self.textfield.delegate = self;
    self.textfield.hidden = YES;
    [self.contentView addSubview:self.textfield];

    self.line = [UILabel new];
    self.line.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:self.line];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    if ((self.section == 1 && self.row == 0) ||self.section == 4 || self.section == 5) {
        self.textfield.hidden = YES;
        
        [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(20));
            make.centerY.mas_equalTo(self.contentView.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(200, 20));
        }];
        [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.contentView.mas_right).with.offset(-20);
            make.centerY.mas_equalTo(self.contentView.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(200, 20));
        }];
    }else if (self.section == 1 && self.row == 1) {
        
        self.textfield.hidden = NO;
        
        [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(20));
            make.centerY.mas_equalTo(self.contentView.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(200, 20));
        }];
        
        self.textfield.textAlignment = NSTextAlignmentRight;
        self.textfield.placeholder = @"输入服务的内容";
        [self.textfield  mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.contentView).with
            .offset(-20);
            make.centerY.mas_equalTo(self.contentView.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(160, 30));
        }];
    }else if(self.section == 3 && self.row == 0){
        
        self.textfield.hidden = YES;
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(20));
            make.centerY.mas_equalTo(self.contentView.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(200, 20));
        }];
        
//        [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.right.mas_equalTo(self.contentView.mas_right).with.offset(-20);
//            make.centerY.mas_equalTo(self.contentView);
//            make.size.mas_equalTo(CGSizeMake(7, 12));
//        }];
    }else if (self.section == 3 && self.row == 1){
        
        self.textfield.hidden = NO;
        self.title.hidden = YES;
        self.name.hidden = YES;
        
        self.textfield.textAlignment = NSTextAlignmentLeft;
        self.textfield.placeholder = @"填写金额";
        [self.textfield  mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(@(20));
            make.centerY.mas_equalTo(self.contentView.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(160, 30));
        }];
    }}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
  
    if (_delegate && [_delegate respondsToSelector:@selector(section:content:)]) {
            [_delegate  section:self.section content:self.textfield.text];
        }
    [textField resignFirstResponder];
    return YES;
}

@end
